package com.example.takeaway.api;

import com.example.takeaway.common.R;
import com.example.takeaway.constant.OrderStatusEnum;
import com.example.takeaway.model.PageVO;
import com.example.takeaway.model.dto.OrderSubmitDTO;
import com.example.takeaway.model.dto.ShopOrderPageQuery;
import com.example.takeaway.model.vo.OrderListVO;
import com.example.takeaway.model.vo.OrderReviewVO;
import com.example.takeaway.model.vo.OrderVO;
import com.example.takeaway.service.OrderService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author 邢晨旭
 * @since 2023-10-12
 */
@RestController
@RequestMapping("/order")
public class OrderApi {

    @Resource
    OrderService orderService;

    @GetMapping("review/{shopId}")
    public R<OrderReviewVO> review(@PathVariable("shopId") Long shopId) {
        //todo 从token中获取用户id
        return R.ok(orderService.review(shopId, 111L));
    }

    @PostMapping("/submit")
    public R<Long> submit(@RequestBody OrderSubmitDTO orderSubmit) {
        //todo 从token中获取用户id
        return R.ok(orderService.submit(orderSubmit));
    }

    /**
     * @param status 1 全部 /2 进行中/3 已完成(待评价) /4 已取消(退款)
     */
    @GetMapping("/list")
    public R<List<OrderListVO>> userOrderList(@RequestParam Integer status) {
        //todo 从token中获取用户id
        return R.ok(orderService.userOrderList(111L, status));
    }

    @GetMapping("/shop/list")
    public R<PageVO<OrderVO>> shopOrderList(ShopOrderPageQuery pageQuery) {
        return R.ok(orderService.shopOrderList(pageQuery));
    }

    @PostMapping("/shop/take/{orderId}")
    public R<Void> takeOrder(@PathVariable("orderId") Long orderId) {
        orderService.changeOrderStatus(orderId, OrderStatusEnum.PREPARING.getCode());
        return R.ok();
    }

    @PostMapping("/shop/ready/{orderId}")
    public R<Void> orderReady(@PathVariable("orderId") Long orderId) {
        orderService.changeOrderStatus(orderId, OrderStatusEnum.DELIVERING.getCode());
        return R.ok();
    }

    @PostMapping("/shop/finish/{orderId}")
    public R<Void> orderFinish(@PathVariable("orderId") Long orderId) {
        orderService.changeOrderStatus(orderId, OrderStatusEnum.FINISHED.getCode());
        return R.ok();
    }

    @GetMapping("/shop/detail/{orderId}")
    public R<OrderVO> orderDetail(@PathVariable("orderId") Long orderId) {
        return R.ok(orderService.orderDetail(orderId));
    }

    @PutMapping("/shop/cancel")
    public R<Void> orderCancel(@RequestParam("orderId") Long orderId,@RequestParam("reason") String reason) {
        orderService.cancelOrder(orderId,reason);
        return R.ok();
    }

}
